package com.springboot_gradrecruit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot_gradrecruit.common.PageResponse;
import com.springboot_gradrecruit.common.R;
import com.springboot_gradrecruit.model.dto.send.PageSendRequest;
import com.springboot_gradrecruit.model.entity.Send;
import com.springboot_gradrecruit.model.vo.job.JobVO;
import com.springboot_gradrecruit.model.vo.student.StudentVO;
import com.springboot_gradrecruit.service.SendService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zr
 */
@Tag(name = "投递相关接口")
@RestController
@RequestMapping("/send")
public class SendController {


    @Resource
    private SendService sendService;

    //增加
    @Operation(summary = "添加投递")
    @PostMapping("/addSend")
    public R<Boolean> addSend(Long jobId, Long studentId,Long enterpriseId) {

        //1. 参数校验
        if (jobId == null || studentId == null|| enterpriseId==null) {
            throw new RuntimeException("参数为空");
        }

        //2. 调用service完成添加
        Send send = new Send();
        send.setJobId(jobId);
        send.setStudentId(studentId);
        send.setEnterpriseId(enterpriseId);
        send.setSendStatus("未回复");
        return sendService.save(send)?R.success(true,"投递成功"):R.error(false,"投递失败");



    }

    //删除
    @Operation(summary = "取消投递")
    @PostMapping("/deleteSend")
    public  R<Boolean> deleteSend(Long jobId, Long studentId,Long enterpriseId) {

        //1. 参数校验
        if (jobId == null || studentId == null) {
            throw new RuntimeException("参数为空");
        }

        //2. 调用service完成删除
        QueryWrapper<Send> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("job_id", jobId);
        queryWrapper.eq("student_id", studentId);
        return sendService.remove(queryWrapper)?R.success(true,"取消成功"):R.error(false,"取消失败");
    }

    //查看
    @Operation(summary = "查看是否已投递")
    @PostMapping("/querySend")
    public R<Boolean> querySend(Long jobId, Long studentId) {

        //1. 参数校验
        if (jobId == null || studentId == null) {
            throw new RuntimeException("参数为空");
        }

        //2. 调用service完成查询
        QueryWrapper<Send> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("job_id", jobId);
        queryWrapper.eq("student_id", studentId);
        return sendService.exists(queryWrapper)?R.success(true,"已投递"):R.success(false,"未投递");
    }

    @Operation(summary = "分页查询已收到投递的岗位")
    @PostMapping("/pageSendJob")
    public R<PageResponse<JobVO>> pageSendJob(@RequestBody PageSendRequest pageSendRequest, HttpServletRequest request) {
        //1. 参数校验
        if(pageSendRequest==null||request==null){
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成查询
        PageResponse<JobVO> pageStudent=sendService.pageSendJob(pageSendRequest);
        return R.success(pageStudent,"查询成功");

    }

    @Operation(summary = "分页查询已投递的学生")
    @PostMapping("/pageSendStudent")
    public R<PageResponse<StudentVO>> pageSendStudent(@RequestBody PageSendRequest pageSendRequest, HttpServletRequest request) {
        //1. 参数校验
        if(pageSendRequest==null||request==null){
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成查询
        PageResponse<StudentVO> pageStudent=sendService.pageSendStudent(pageSendRequest);
        return R.success(pageStudent,"查询成功");

    }


    @Operation(summary = "查询投递的数据量")
    @PostMapping("/querySendCount")
    public R<Long> querySendCount(Long enterpriseId,String sendStatus) {

        //1. 参数校验
        if (enterpriseId==null||sendStatus==null) {
            throw new RuntimeException("参数为空");
        }

        //2. 调用service完成查询
        QueryWrapper<Send> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enterprise_id", enterpriseId);
        queryWrapper.eq("send_status", sendStatus);
        return R.success(sendService.count(queryWrapper),"查询成功");
    }

    //修改投递状态
    @Operation(summary = "修改投递状态")
    @PostMapping("/updateSendStatus")
    public R<Boolean> updateSendStatus(Long jobId, Long studentId,String sendStatus) {

        //1. 参数校验
        if (jobId == null || studentId == null||sendStatus==null) {
            throw new RuntimeException("参数为空");
        }

        //2. 调用service完成修改
        QueryWrapper<Send> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("job_id", jobId);
        queryWrapper.eq("student_id", studentId);
        Send send =new Send();
        send.setSendStatus(sendStatus);
        return sendService.update(send,queryWrapper)?R.success(true,"修改成功"):R.error(false,"修改失败");
    }



}
